package a03_哈希;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * a05_四数相加2
 * </p>
 *
 * @author flyduck
 * @since 2024-06-25
 */
public class a06_四数相加2复习3 {
    public static void main(String[] args) {
        int[] nums1 = {1,2};
        int[] nums2 = {-2,-1};
        int[] nums3 = {-1,2};
        int[] nums4 = {0,2};
        System.out.println(fourSumCount(nums1, nums2, nums3, nums4));
    }

    public static int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                if(map.containsKey(nums1[i] + nums2[j])){
                    map.put(nums1[i] + nums2[j],map.get(nums1[i] + nums2[j]) + 1);
                }else{
                    map.put(nums1[i] + nums2[j], 1);
                }
            }
        }

        int count = 0;
        for (int i = 0; i < nums3.length; i++) {
            for (int j = 0; j < nums4.length; j++) {
                if(map.containsKey(0 - (nums3[i] + nums4[j]))){
                    count = count + map.get(0 - (nums3[i] + nums4[j]));
                }
            }
        }
        return count;
    }
}
